home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13916 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: interramp.com!usenet
  2. From: us011245@interramp.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: when to inherit, when to compose...
  5. Date: Tue, 26 Mar 96 18:20:56 PDT
  6. Organization: PSI Public Usenet Link
  7. Message-ID: <NEWTNews.17513.827895367.hampton@interramp.com>
  8. References: <31587163.1045@datalytics.com>
  9. NNTP-Posting-Host: ip216.herndon7.va.interramp.com
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=US-ASCII
  12. X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
  13.  
  14.  
  15. In article <31587163.1045@datalytics.com>, <stew@datalytics.com> writes:
  16. > John Peter Lee wrote:
  17. > > 
  18. > > Hi:
  19. > > 
  20. > >   I'm wondering about an OO design. Under what circumstances is it more
  21. > >   advantageous to use an inheritance hierarchy for a design, as opposed
  22. > >   to using object containment, and vice versa?
  23. > > 
  24. > >   With object containment: class "A" has a member class "B" declared
  25. > >   within it. A acts as a kindof handle class (I think) that forwards
  26. > >   requests to B, and handles B's output.
  27. > > 
  28. > >   With inheritance: class "A" is a base class, and class "B" is a derived
  29. > >   class. Then A has virtual functions that are implemented in B.
  30. > > 
  31. > >   -> Are there any general rules of thumb to apply here? Any literature?
  32. > > 
  33. > The obvious one is correct, but not always easy to recognize: 
  34. > inheritance applies only when the derived class is a form of the 
  35. > base class, you inherit.  When it just happens to need to do 
  36. > things another class offers, you use the other class.  Those are 
  37. > easy statements to make.  They aren't so easy to practice.
  38. > It really comes down to deciding if everything about the other 
  39. > class is just like your new class except your new class adds a 
  40. > few twists of its own.  This is when you use inheritance.  If 
  41. > the other class has things that aren't really right for your new 
  42. > class, then you probably need to use the other class.  However, 
  43. > you might also want to consider creating a common base class so 
  44. > that your new class and that other class derive from it.
  45. >
  46. What you're talking about is sometimes called delegation.  It works like 
  47. this: if I want to implement a stack class, and easy way to do it is to use 
  48. some existing class.  Suppose that I have a list class already.  I can 
  49. implement the stack by using the list class operations.  In particular, I can 
  50. implement push by adding an element to the top of the list and pop by removing 
  51. the element at the top of the list.  
  52.  
  53. OK that's cool.  The question is, should my stack class inherit from List as a 
  54. parent class, or should I just hide a list in my stack object and write push 
  55. and pop operations which manipulate this list out of sight of the user.  That's 
  56. the rub.  As you can see, if I use inheritance to implement the stack class 
  57. from the list class, the user will have access to all the public operations of 
  58. a list, such as sticking things on the end of the list or even (gasp!) in the 
  59. middle of the list.  Great behavior for a list but uncool behavior for a stack. 
  60.  If I use delegation instead, that is, I hide a list in my stack class and 
  61. provide only push and pop operations to the public, then objects using my stack 
  62. class will have no access to the underlying list operations, they won't even 
  63. know that a list is there.  The decision therefore as to whether to use 
  64. inheritance or delegation will often revolve around whether the user requires 
  65. access to the underlying implementation and whether that access is consistent 
  66. with the class you're trying to implement.  I.e., if it makes sense for a user 
  67. of class X to use class Y's public operations, then inherit from class Y, 
  68. otherwise, hide an object of class Y in class X and use private operations on 
  69. the hidden class to implement class X's operations.
  70.  
  71. Regards, 
  72. Luther Hampton       
  73.  
  74.  
  75.